package com.cyj.controller;

import com.cyj.entity.Anime;
import com.cyj.entity.AnimeType;
import com.cyj.entity.FileEntity;
import com.cyj.service.AnimeTypeService;
import com.github.pagehelper.PageInfo;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

@Controller
@RequestMapping("/type")
public class AnimeTypeController {

    @Autowired
    private AnimeTypeService animeTypeService;

    @RequestMapping("/types")
    public String types(Locale locale){

        return "animeTypeList";
    }

    @ResponseBody
    @RequestMapping(value = "/animeTypeList",produces = "application/json;charset=utf-8")
    public Map AnimeTypeList(@RequestParam(value = "pn",defaultValue = "1")Integer pn, Model model) throws IOException {
        Map map = new HashMap<>();

        PageInfo page  = animeTypeService.selectAnimeTypeList(map,pn,5);

        map.put("pageInfo",page);
        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/allAnimeTypeList",produces = "application/json;charset=utf-8")
    public Map allAnimeTypeList() throws IOException {
        Map<String,Object> map = new HashMap<String,Object>();

        List<AnimeType> list = animeTypeService.allAnimeTypeList(map);
        if(list.size()>0){
            map.put("code",200);
            map.put("msg","动漫类型查找成功");
            map.put("data",list);
        }else{
            map.put("code",100);
            map.put("msg","动漫类型查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/typeBy",produces = "application/json;charset=utf-8")
    public Map typeBy(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        Integer typeId = Integer.parseInt(request.getParameter("typeId"));

        map.put("typeId",typeId);

        AnimeType animeType = animeTypeService.selectAnimeType(map);

        if(animeType!=null){
            map.put("code", 200);
            map.put("msg", "动漫类型查找成功");
            map.put("data",animeType);
        }else {
            map.put("code", 100);
            map.put("msg", "动漫类型查找失败");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/updateAnimeType",produces = "application/json;charset=utf-8")
    public Map updateAnimeType(HttpServletRequest request, FileEntity entity){

        Map<String, String[]> map = request.getParameterMap();
        AnimeType animeType = new AnimeType();

        try {
            BeanUtils.populate(animeType,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        System.out.println(animeType);

        int num = animeTypeService.updateAnimeType(animeType);

        Map<String,Object> map2 = new HashMap<String,Object>();

        if (num>0){
            map2.put("code", 200);
            map2.put("msg", "动漫信息修改成功");
        }else {
            map2.put("code", 100);
            map2.put("msg", "动漫信息修改失败");
        }

        return map2;
    }

    @ResponseBody
    @RequestMapping(value = "/addAnimeType",produces = "application/json;charset=utf-8")
    public Map addAnimeType(@RequestBody AnimeType animeType){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(animeType);

        int num = animeTypeService.addAnimeType(animeType);


        if(num>0){
            map.put("code",200);
            map.put("msg","添加动漫类型成功！");
        }else{
            map.put("code",100);
            map.put("msg","添加动漫类型失败！");
        }

        return map;
    }

    @ResponseBody
    @RequestMapping(value = "/delAnimeType",produces = "application/json;charset=utf-8")
    public Map delAnimeType(@RequestBody AnimeType animeType){

        Map<String,Object> map = new HashMap<String,Object>();

        System.out.println(animeType);

        int num = animeTypeService.deleteAnimeType(animeType.getTypeId());

        if(num>0){
            map.put("code",200);
            map.put("msg","动漫类型删除成功！");
        }else{
            map.put("code",100);
            map.put("msg","动漫类型删除失败！");
        }


        return map;
    }
}
